package com.atguigu.gmallsugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmallsugar.pojo.SpuStats;
import com.atguigu.gmallsugar.pojo.VisitStats;
import com.atguigu.gmallsugar.service.KwStatsServiceImpl;
import com.atguigu.gmallsugar.service.ProductStatsServiceImpl;
import com.atguigu.gmallsugar.service.VisitorServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class Controller {
    @Autowired
    ProductStatsServiceImpl service;

    public int defaultDate(int date) {
        if (date == 0) {
            //说明请求没传日期,默认用当天日期
            date = Integer.parseInt(new SimpleDateFormat("YYYYMMdd").format(new Date()));
        }
        return date;
    }

    @RequestMapping(value = "/sugar/gmv")
    public String gmv(@RequestParam(value = "date", defaultValue = "0") int date) {
        //请求参数RequestParam注解,传入参数名和默认值,默认值只能用string
        date = defaultDate(date);
        JSONObject gmv = new JSONObject();
        gmv.put("status", 0);
        gmv.put("msg", "");

        BigDecimal gmvFromCK = service.getGMVFromCK(date);
        gmv.put("date", gmvFromCK);

        return gmv.toString();
    }

    @RequestMapping(value = "/sugar/spu_gmv")
    public String gmv(@RequestParam(value = "date", defaultValue = "0") int date,
                      @RequestParam(value = "limit", defaultValue = "5") int limit) {
        //请求参数RequestParam注解,传入参数名和默认值,默认值只能用string
        date = defaultDate(date);
        JSONObject gmv = new JSONObject();
        gmv.put("status", 0);
        gmv.put("msg", "");
        JSONObject data = new JSONObject();
        JSONArray columns = new JSONArray();
        data.put("columns", columns);
        JSONObject c1 = new JSONObject();
        c1.put("name", "spu名字");
        c1.put("id", "spu_name");
        columns.add(c1);
        JSONObject c2 = new JSONObject();
        c2.put("name", "spu金额");
        c2.put("id", "order_amount");
        columns.add(c2);
        List<SpuStats> gmvBySpu = service.getGMVBySpu(date, limit);
        JSONArray rows = new JSONArray();

        for (SpuStats spuStats : gmvBySpu) {
            JSONObject circle = new JSONObject();
            circle.put("spu_name", spuStats.spu_name);
            circle.put("order_amount", spuStats.order_amount);
            rows.add(circle);
        }

        data.put("rows", rows);
        gmv.put("data", data);
        return gmv.toString();
    }

    @Autowired
    VisitorServiceImpl visitorService;

    @RequestMapping(value = "/sugar/byHour")
    public String byHour(@RequestParam(value = "date", defaultValue = "0") int date) {
        //请求参数RequestParam注解,传入参数名和默认值,默认值只能用string
        date = defaultDate(date);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");

        JSONObject data = new JSONObject();
        JSONArray categories = new JSONArray();
        for (int i = 0; i < 24; i++) {
            categories.add(new DecimalFormat("00").format(i));
        }
        data.put("categories", categories);

        JSONArray series = new JSONArray();
        List<VisitStats> visitStats = visitorService.statsByHour(date);


        JSONObject pv = new JSONObject();
        pv.put("name", "pv");

        JSONArray dataByHour = new JSONArray();
        HashMap<String, VisitStats> map = new HashMap<>();
        for (VisitStats visitStat : visitStats) {
            map.put(visitStat.hour, visitStat);
        }
        for (int i = 0; i < 24; i++) {
            VisitStats visitStats1 = map.get(i + "");
            if (visitStats1 != null) {
                dataByHour.add(visitStats1.getPv());

            } else {
                dataByHour.add(0);
            }
        }
        pv.put("date", dataByHour);
        series.add(pv);


        data.put("series", series);
        result.put("data", data);
        return result.toString();
    }

    @Autowired
    KwStatsServiceImpl kwStatsService;
    @RequestMapping(value = "/sugar/kw")
    public String kw(@RequestParam(value = "date", defaultValue = "0") int date,
                     @RequestParam(value = "limit", defaultValue = "5") int limit) {
        //请求参数RequestParam注解,传入参数名和默认值,默认值只能用string
        date = defaultDate(date);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        Map<String, BigInteger> map = kwStatsService.kw(date, limit);
        JSONArray data = new JSONArray();

        Set<Map.Entry<String, BigInteger>> entries = map.entrySet();
        for (Map.Entry<String, BigInteger> entry : entries) {
            JSONObject o1 = new JSONObject();
            o1.put("name", entry.getKey());
            o1.put("value", entry.getValue());
            data.add(o1);
        }
        result.put("data", data);
        return result.toString();
    }
}
